package com.sskj.aikeeper.hangqing.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.bean.CoinDescribeBean;
import com.sskj.aikeeper.presenter.ChartPresenter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.KChartPop;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/07/08
 */
@Route(path = RConfig.APP_ACTIVITY_CHART)
public class ChartActivity extends BaseActivity<ChartPresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R.id.tvHigh)
    TextView tvHigh;
    @BindView(R.id.tvRMB)
    TextView tvRMB;
    @BindView(R.id.tvRate)
    TextView tvRate;
    @BindView(R.id.tvLow)
    TextView tvLow;
    @BindView(R.id.chartTabLayout)
    CommonTabLayout chartTabLayout;
    @BindView(R.id.tvIndicator)
    TextView tvIndicator;
    @BindView(R.id.tv_coin_name)
    TextView tvCoinName;
    @BindView(R.id.tv_release_time)
    TextView tvReleaseTime;
    @BindView(R.id.tv_release_all)
    TextView tvReleaseAll;
    @BindView(R.id.tv_liutong)
    TextView tvLiutong;
    @BindView(R.id.tv_white_book)
    TextView tvWhiteBook;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @Autowired
    String coinCode;
    @Autowired
    CoinBean coinBean;

    //K线
    ArrayList<CustomTabEntity> chartTabs = new ArrayList<>();
    private String[] goodsType = {"minute", "minute1", "minute5", "minute15", "minute30", "day"};
    private ArrayList<ChartFragment> chartFragments = new ArrayList();
    private int currentIndex;

    //指标
    private boolean isUpToggle = true;
    private boolean isDownToggle = true;
    private KChartPop kChartPop;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_chart;
    }

    @Override
    public ChartPresenter getPresenter() {
        return new ChartPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        tvTitle.setText(coinCode);
        chartTabs.add(new TabItem(App.INSTANCE.getString(R.string.app_chartActivity1), 0, 0));
        chartTabs.add(new TabItem("1M", 0, 0));
        chartTabs.add(new TabItem("5M", 0, 0));
        chartTabs.add(new TabItem("15M", 0, 0));
        chartTabs.add(new TabItem("30M", 0, 0));
        chartTabs.add(new TabItem(App.INSTANCE.getString(R.string.app_chartActivity2), 0, 0));
        chartTabLayout.setTabData(chartTabs);
    }

    @Override
    public void initData() {
        initTab();
        initTopData(coinBean);
        initPoint();
        mPresenter.getCoinDescribe(coinCode);

    }

    private void initTopData(CoinBean data) {
        tvUSDT.setText(NumberUtil.keep2(data.getPrice()));
//        tvRMB.setText(String.format("≈%s CNY", CommonUtil.dealCNY(data.getCnyPrice())));
        tvRMB.setText(String.format("≈%s CNY", NumberUtil.keepDown(data.getCnyPrice(), 2, true)));
        tvRate.setText(data.getChangeRate());
        tvLow.setText(NumberUtil.keep2(data.getLow()));
        tvHigh.setText(NumberUtil.keep2(data.getHigh()));
        tvUSDT.setTextColor(ContextCompat.getColor(App.INSTANCE, data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
        tvRate.setTextColor(ContextCompat.getColor(App.INSTANCE, data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
    }

    private void initPoint() {
        ClickUtil.click(tvIndicator, () -> {
            if (kChartPop == null) {
                kChartPop = new KChartPop(this);
                View contentView = kChartPop.getContentView();
                TextView tvMa = contentView.findViewById(R.id.tvMa);
                TextView tvBoll = contentView.findViewById(R.id.tvBoll);
                TextView tvMacd = contentView.findViewById(R.id.tvMacd);
                TextView tvKdj = contentView.findViewById(R.id.tvKdj);
                TextView tvRsi = contentView.findViewById(R.id.tvRsi);
                TextView tvWr = contentView.findViewById(R.id.tvWr);
                ImageView ivUpToggle = contentView.findViewById(R.id.ivUpToggle);
                ImageView ivDownToggle = contentView.findViewById(R.id.ivDownToggle);
                ivUpToggle.setImageResource(R.mipmap.icon_xianshi);
                ivDownToggle.setImageResource(R.mipmap.icon_xianshi);
                ClickUtil.click(tvMa, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setMainDrawMaShow();
                    ivUpToggle.setImageResource(R.mipmap.icon_xianshi);
                    kChartPop.dismiss();
                });
                ClickUtil.click(tvBoll, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setMainDrawBollShow();
                    ivUpToggle.setImageResource(R.mipmap.icon_xianshi);
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivUpToggle, () -> {
                    if (isUpToggle) {
                        isUpToggle = false;
                        ivUpToggle.setImageResource(R.mipmap.icon_yincang);

                        chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setMainDrawNoneShow();
                    } else {
                        isUpToggle = true;
                        ivUpToggle.setImageResource(R.mipmap.icon_xianshi);
                        chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setMainDrawMaShow();
                    }
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvMacd, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().changeMACD();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvKdj, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().changeKDJ();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvRsi, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().changeRSI();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvWr, () -> {
                    chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().changeWR();
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivDownToggle, () -> {
                    if (isDownToggle) {
                        isDownToggle = false;
                        ivDownToggle.setImageResource(R.mipmap.icon_yincang);
                        chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setDrawDown(isDownToggle);
                    } else {
                        isDownToggle = true;
                        ivDownToggle.setImageResource(R.mipmap.icon_xianshi);

                        chartFragments.get(chartTabLayout.getCurrentTab()).getKChartView().setDrawDown(isDownToggle);
                    }
                    kChartPop.dismiss();

                });
                kChartPop.setBackground(0);
            }
            if (kChartPop.isShowing()) {
                kChartPop.dismiss();
            } else {
                kChartPop.showPopupWindow(chartTabLayout);
            }
        });

    }

    private void initTab() {

        chartFragments.add(null);
        chartFragments.add(null);
        chartFragments.add(null);
        chartFragments.add(null);
        chartFragments.add(null);
        chartFragments.add(null);
        chartTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectTab(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        selectTab(0);
    }

    private void selectTab(int index) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ChartFragment nextFragment = chartFragments.get(index);
        ChartFragment currentFragment = chartFragments.get(currentIndex);
        if (currentFragment != null) {
            ft.hide(currentFragment);
        }
        if (nextFragment == null) {
            if (index == 0) {
                nextFragment = (ChartFragment) ARouter.getInstance().build(RConfig.APP_FRAGMENT_CHART)
                        .withString(Constans.COIN_CODE, coinCode)
                        .withString(Constans.COIN_GOODS, goodsType[index])
                        .withBoolean(Constans.COIN_ISMINUTE, true)
                        .navigation();
            } else {
                nextFragment = (ChartFragment) ARouter.getInstance().build(RConfig.APP_FRAGMENT_CHART)
                        .withString(Constans.COIN_CODE, coinCode)
                        .withString(Constans.COIN_GOODS, goodsType[index])
                        .withBoolean(Constans.COIN_ISMINUTE, false)
                        .navigation();
            }
            chartFragments.set(index, nextFragment);
            ft.add(R.id.frameLayoutChart, nextFragment);
        } else {
            ft.show(nextFragment);
        }
        ft.commitAllowingStateLoss();
        currentIndex = index;
    }

    @SuppressLint("CheckResult")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshCoin(CoinBean data) {
        if (data == null) {
            return;
        }
        if (!coinCode.equals(data.getCode())) {
            return;
        }
        initTopData(data);
    }

    public void getCoinDescribeOK(CoinDescribeBean data) {
        if (data == null) {
            return;
        }
        tvCoinName.setText(data.getPname());
        tvReleaseTime.setText(data.getFxtime());
        tvReleaseAll.setText(data.getFxnum());
        tvWhiteBook.setText(data.getFxbook());
        RichText.from(data.getMemo()).into(tvMsg);
    }
}
